<?php
/**
 * 静态标签选项卡Widgets
 * 所谓静态，就是没有JS效果，页面是会跳转的，只是样子比较像动态的标签选项卡而已
 */

namespace common\widgets;



use yii\helpers\Html;

class StaticTab extends Widget {

    /*
     * tab数组
     * [
     *      ['id'=>'id1', 'title'=>'title1', 'url'=>'url1'],
     *      ['id'=>'id2', 'title'=>'title1', 'url'=>'url2'],
     *      ...
     * ]
     */
    public $tabs;

    /*
     * 当前ID
     */
    public $current;



    public function init(){
        parent::init();
        if(empty($this->options['class']))
            $this->options['class'] = 'nav-tabs-custom';
        echo '<div class="'.$this->options['class'].'">'.PHP_EOL;
        $this->renderTabHeader();
        echo '<div class="tab-content">'.PHP_EOL;
        echo '<div role="tabpanel" class="tab-pane active" id="'.$this->current.'">'.PHP_EOL;
    }

    public function run(){
        parent::run();
        echo '</div>'.PHP_EOL
             . '</div>'.PHP_EOL
             . '<div class="clearfix"></div>'.PHP_EOL
             . '</div>';
    }

    /*
     * 渲染Tab选项卡的头部
     */
    public function renderTabHeader(){
        echo '<ul class="nav nav-tabs" role="tablist">';
        foreach($this->tabs as $k => $tab){
            if(array_key_exists('display', $tab) && empty($tab['display'])){

            }else{
                $a = Html::a($tab['title'], $tab['url'], ['aria-controls'=>$tab['id'], 'role'=>'tab']);
                if($tab['id'] == $this->current){
                    echo '<li role="presentation" class="active">'.$a.'</li>'.PHP_EOL;
                }else{
                    echo '<li role="presentation">'.$a.'</li>'.PHP_EOL;
                }
            }
        }
        echo '</ul>'.PHP_EOL;
    }

}